﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DTO;
using DAO;
using BUS;

namespace QLBANSACH
{
    public partial class HDX : Form
    {
        SqlConnection connection = DAO.sqlConnectionData.Hamketnoi();

        public HDX()
        {
            InitializeComponent();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Hide();
            CTHDX cthdx = new CTHDX();
            cthdx.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                HDX_DTO hdx = new HDX_DTO(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text), Convert.ToDateTime(textBox3.Text));
                HDX_BUS.InsertHDX(hdx);
                dataGridView1.DataSource = HDX_BUS.HienthiHDX();
                databinding();
                MessageBox.Show("Thêm thành công!");
            }
            catch
            {
                MessageBox.Show("Thêm thất bại!");
                dataGridView1.DataSource = HDX_BUS.HienthiHDX();
                databinding();
            }
        }

        public void databinding()
        {
            textBox1.DataBindings.Clear();
            textBox1.DataBindings.Add("Text", dataGridView1.DataSource, "ID_HD");
            textBox2.DataBindings.Clear();
            textBox2.DataBindings.Add("Text", dataGridView1.DataSource, "SL_DH");
            textBox3.DataBindings.Clear();
            textBox3.DataBindings.Add("Text", dataGridView1.DataSource, "TG_XS");
        }

        private void HDX_Load(object sender, EventArgs e)
        {
            connection.Open();
            dataGridView1.DataSource = HDX_BUS.HienthiHDX();
            databinding(); 
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                HDX_DTO hdx = new HDX_DTO(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text), Convert.ToDateTime(textBox3.Text));
                HDX_BUS.UpdateHDX(hdx);
                dataGridView1.DataSource = HDX_BUS.HienthiHDX();
                databinding();
                MessageBox.Show("Sửa thành công!");
            }
            catch
            {
                MessageBox.Show("Sửa thất bại!");
                dataGridView1.DataSource = HDX_BUS.HienthiHDX();
                databinding();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                HDX_DTO hdx = new HDX_DTO(Convert.ToInt32(textBox1.Text));
                HDX_BUS.DeleteHDX(hdx);
                dataGridView1.DataSource = HDX_BUS.HienthiHDX();
                MessageBox.Show("Xóa thành công!");
            }
            catch
            {
                MessageBox.Show("Xóa thất bại!");
            }
        }
    }
}
